package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.sun.org.apache.xpath.internal.operations.Or;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/orderSetting")
@Slf4j
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;

    @RequestMapping("/upload")
    public Result upload(MultipartFile excelFile){
        log.debug("OrSettingController: upload: excelFile ===> " + excelFile.toString());
        List<OrderSetting> orderSettingList = new ArrayList<OrderSetting>();
        try {
            InputStream inputStream = excelFile.getInputStream();
            XSSFWorkbook xssfWorkbook = new XSSFWorkbook(inputStream);
            XSSFSheet sheet = xssfWorkbook.getSheetAt(0);
            int lastRowNum = sheet.getLastRowNum();
            System.out.println("lastRowNum ==== " + lastRowNum);
            for (int i = 1; i <= lastRowNum; i++) {
                OrderSetting orderSetting = new OrderSetting();
                XSSFRow row = sheet.getRow(i);
                XSSFCell cell = row.getCell(0);
                Date dateCellValue = cell.getDateCellValue();

                orderSetting.setOrderDate(dateCellValue);

                XSSFCell cell1 = row.getCell(1);
                if (cell1.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                    cell1.setCellType(Cell.CELL_TYPE_STRING);
                }
                String stringCellValue = cell1.getStringCellValue();
                int parseInt = Integer.parseInt(stringCellValue);
                orderSetting.setNumber(parseInt);

                orderSettingList.add(orderSetting);
            }
            log.debug("OrSettingController: upload: orderSettingList ===> " + orderSettingList);
            orderSettingService.add(orderSettingList);
            log.debug("批量导入预约设置数据成功");
            return new Result(true, MessageConst.IMPORT_ORDERSETTING_SUCCESS);
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/findByDate")
    public Result findByDate(String date){
        log.debug("OrderSettingController: findByDate: date ===> " + date);
        List<OrderSetting> orderSettingList = orderSettingService.findByDate(date);
        log.debug("OrderSettingController: findByDate: orderSettingList ===> " + orderSettingList);
        List<Map<String,Object>> mapList = new ArrayList<Map<String, Object>>();
        for (OrderSetting orderSetting : orderSettingList) {
            Map<String, Object> map = new HashMap<>();
            map.put("date",new SimpleDateFormat("dd").format(orderSetting.getOrderDate()));
            map.put("number",orderSetting.getNumber());
            map.put("reservations",orderSetting.getReservations());

            mapList.add(map);
        }
        log.debug("OrderSettingController: findByDate: mapList ===> " + mapList);
        log.debug("查询预约信息成功");
        return new Result(true,MessageConst.QUERY_ORDER_SUCCESS,mapList);
    }

    @RequestMapping("/addOrEdit")
    public Result addOrEdit(@RequestBody OrderSetting orderSetting){
        log.debug("OrderSettingController: addOrEdit: orderSetting ===> " + orderSetting);
        orderSettingService.addOrEdit(orderSetting);
        log.debug("修改预约设置信息成功");
        return new Result(true,"修改预约设置信息成功");
    }

}
